package com.example.sl;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class DataBaseActivity extends AppCompatActivity implements View.OnClickListener {
    TextView textView;
    String mDataBaseName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_base);

        Button button1 = findViewById(R.id.btn_create);
        Button button2 = findViewById(R.id.btn_delete);
        textView = findViewById(R.id.tv1);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);

        mDataBaseName = getFilesDir() + "/test.db";

    }

    @Override
    public void onClick(View v) {
        String desc;
        int id = v.getId();
        if(id == R.id.btn_create){

            SQLiteDatabase db = openOrCreateDatabase(mDataBaseName, MODE_PRIVATE, null);
            desc = String.format("数据库%s创建%s",mDataBaseName,(db == null)?"失败":"成功");
            textView.setText(desc);

        }else if(id == R.id.btn_delete){
            boolean b = deleteDatabase(mDataBaseName);
            desc = String.format("数据库删除%s",b?"成功":"失败");
            textView.setText(desc);
        }
    }
}